﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TasksUI.Controls
{
    public partial class SimpleTaskControl : UserControl
    {
        public SimpleTaskControl()
        {
            InitializeComponent();
            base.Disposed += new EventHandler(SimpleTaskControl_Disposed);
        }

        private void SimpleTaskControl_Disposed(object sender, EventArgs e)
        {
            if (_task != null)
                _task.PropertyChanged -= new PropertyChangedEventHandler(_task_PropertyChanged);
        }

        private TasksModel.Task _task;

        [DefaultValue(null)]
        public TasksModel.Task Task
        {
            [System.Diagnostics.DebuggerStepThrough]
            get { return _task; }
            set
            {
                if (value == null)
                    return;
                if (!object.ReferenceEquals(_task, value))
                {
                    if (_task != null)
                        _task.PropertyChanged -= new PropertyChangedEventHandler(_task_PropertyChanged);
                    _task = value;
                    _task.PropertyChanged += new PropertyChangedEventHandler(_task_PropertyChanged);
                    RefreshView();
                }
            }
        }

        private void _task_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Name" || e.PropertyName == "DueDate")
                RefreshView();
        }

        private static readonly int _year = DateTime.Now.Year;

        private void RefreshView()
        {
            if (Tag != null)
                return;// sentinel
            l1.Text = _task.Name;
            if (_task.DueDate.HasValue)
            {
                if (_task.Progress > 0)
                {
                    if (_task.DueDate.Value.Year == _year)
                        l2.Text = string.Format("Done: {0}% Due: {1}",
                            _task.Progress, _task.DueDate.Value.ToString("dd-MM"));
                    else
                        l2.Text = string.Format("Done: {0}% Due: {1}",
                            _task.Progress, _task.DueDate.Value.ToString("dd-MM-yyyy"));
                }
                else
                {
                    if (_task.DueDate.Value.Year == _year)
                        l2.Text = string.Format("Due: {0}", _task.DueDate.Value.ToString("dd-MM"));
                    else
                        l2.Text = string.Format("Due: {0}", _task.DueDate.Value.ToString("dd-MM-yyyy"));
                }
            }
            else
                if (_task.Progress > 0)
                    l2.Text = string.Format("Done: {0}% Estm: {1}h",
                        _task.Progress, _task.EstimatedEffort);
                else
                    l2.Text = string.Format("Estm: {0}h", _task.EstimatedEffort);
        }

        private void SimpleTaskControl_DoubleClick(object sender, EventArgs e)
        {
            Main main = (Main)TopLevelControl;
            main.ViewTask(Task);
        }
    }
}